package com.hnkywl.sts.service.base;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.base.GspzDao;
import com.hnkywl.sts.entity.base.Gspz;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-20
 * Time: 下午8:49
 * To change this template use File | Settings | File Templates.
 */
@Service
public class GspzService {

    private GspzDao gspzDao;

    public String findGspzList(String mc, int pageNo, int pageSize) {
        Page<Gspz> page = this.gspzDao.findGspzList(mc, pageNo, pageSize);
        List<Gspz> GspzList = page.getResult() == null ? new ArrayList<Gspz>() : page.getResult();
        Map<String, Object> otherParam = new HashMap<String, Object>();
        String GspzJson = BaseUtils.fetchCollectionToJson(GspzList, otherParam, "id", "mc", "szr.xm", "sfqy", "jzrq", "zt", "sfkhfkrq", "khfkrq");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":[" + GspzJson + "]}";
    }

    //显示状态为已启用的公式
    public String findGspzList01(String mc, int pageNo, int pageSize) {
        Page<Gspz> page = this.gspzDao.findGspzList01(mc, pageNo, pageSize);
        List<Gspz> GspzList = page.getResult() == null ? new ArrayList<Gspz>() : page.getResult();
        Map<String, Object> otherParam = new HashMap<String, Object>();
        String GspzJson = BaseUtils.fetchCollectionToJson(GspzList, otherParam, "id", "mc", "szr.xm", "sfqy", "jzrq", "zt", "sfkhfkrq", "khfkrq");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":[" + GspzJson + "]}";
    }

    @Transactional
    public Gspz findGspzById(Long id) {
        return gspzDao.get(id);
    }

    @Transactional
    public void addGspz(Gspz gspz) throws Exception {
        if (gspz.getAtsqList() != null) {
            if (gspz.getSfkhfkrq() == true && gspz.getKhfkrq() != null) {
                throw new RuntimeException("是否客户付款日期和客户付款日期请选择一项!");
            }
            if (gspz.getSfkhfkrq() == false && gspz.getKhfkrq() == null) {
                throw new RuntimeException("是否客户付款日期和客户付款日期请选择一项!");
            }
        }
        gspz.setSzr(YgService.getCurrentYg());
        gspz.setZt("ybc");
        gspz.setSfqy(true);
        gspz.setLastUpdated(new Date());
        gspz.setCreateUser(YgService.getCurrentYg());
        gspz.setCreateOrgan(YgService.getCurrentYg().getZggs());
        gspz.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        gspzDao.save(gspz);
    }


    @Transactional
    public void startGspz(Long gspzId) {
        Gspz gspz = gspzDao.get(gspzId);
        gspz.setSfqy(true);
        gspzDao.save(gspz);
    }

    @Transactional
    public void stopGspz(Long gspzId) throws Exception {
        Boolean f = gspzDao.checkGspzUsed(gspzId);
        if (f) {
            throw new RuntimeException("该公式已被合同使用无法停用!");
        }
        Gspz gspz = gspzDao.get(gspzId);
        gspz.setSfqy(false);
        gspzDao.save(gspz);
    }

    @Transactional
    public String findGspzListByHtId(Long htId, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page<Map> page = gspzDao.findgspzListByHtId(htId, pageNo, pageSize);
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    @Transactional
    public String findAdsqList(Long gspzId, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page<Map> page = gspzDao.findAdsqList(gspzId, pageNo, pageSize);
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    @Transactional
    public String findAtsqList(Long gspzId, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page<Map> page = gspzDao.findAtsqList(gspzId, pageNo, pageSize);
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    public boolean ajaxValidate(Long id, String value, Object field) {
        return gspzDao.checkUnique(id, value, field);
    }


    @Autowired
    public void setGspzDao(GspzDao gspzDao) {
        this.gspzDao = gspzDao;
    }
}
